package org.xqyy.eval.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xqyy.eval.service.IGradeScoreService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxiaoming on 16-12-25.
 */

@Controller
@RequestMapping("/gradeScore")
public class GradeScoreController {

    @Autowired
    private IGradeScoreService gradeScoreService;

    @RequestMapping("/loadGradeScores")
    @ResponseBody
    public Map<String, Object> loadGradeScores(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap();
        List<Map<String, Object>> data = gradeScoreService.load(res);
        res.put("root", data);
        return res;
    }

    @RequestMapping("/addGradeScore")
    @ResponseBody
    public Map<String, Object> addGradeScore(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String code = request.getParameter("code");
            String name = request.getParameter("name");
            String score = request.getParameter("score");
            res.put("code", code);
            res.put("name", name);
            res.put("score", score);
            gradeScoreService.add(res);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/updateGradeScore")
    @ResponseBody
    public Map<String, Object> updateGradeScore(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String code = request.getParameter("code");
            String name = request.getParameter("name");
            String score = request.getParameter("score");
            res.put("code", code);
            res.put("name", name);
            res.put("score", score);
            gradeScoreService.modify(res);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/deleteGradeScore")
    @ResponseBody
    public Map<String, Object> deleteGradeScore(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String code = request.getParameter("code");
            res.put("code", code);
            gradeScoreService.delete(res);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/selectAllGradeScores")
    @ResponseBody
    public List<Map<String, Object>>  selectAllGradeScores(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Object> res = new HashMap();
        res.put("value",20);
        res.put("text","满意");
        mapList.add(res);
        res = new HashMap();
        res.put("value",15);
        res.put("text","一般");
        mapList.add(res);
        res = new HashMap();
        res.put("value",10);
        res.put("text","不满意");
        mapList.add(res);
        return mapList;
    }
}
